/*
This file is part of [aconnect] library. 

Author: Artem Kustikov (kustikoff[at]tut.by)


This code is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this code.

Permission is granted to anyone to use this code for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:

1. The origin of this code must not be misrepresented; you must
not claim that you wrote the original code. If you use this
code in a product, an acknowledgment in the product documentation
would be appreciated but is not required.

2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original code.

3. This notice may not be removed or altered from any source
distribution.
*/

#ifndef ACONNECT_UTIL_GUARDS_H
#define ACONNECT_UTIL_GUARDS_H

#include "types.hpp"
#include "error.hpp"

namespace aconnect { namespace util
{
	typedef void (*simple_callback) ();

	template <typename OnDestroyFun = simple_callback> 
	class scoped_guard : private boost::noncopyable
	{
	public:
		scoped_guard (OnDestroyFun proc) : _proc (proc), _released (false) {  }

		~scoped_guard () {   
			release();
		}

		void release() {
			if (_released)
				return;

			try {
				_proc();
			} catch(...) {
				// eat exception
			}

			_released = false;
		}
	
	private:
		OnDestroyFun _proc;
		bool _released;
	};

    //==================================================================//
	template <typename T> 
	class scoped_memfun_guard : private boost::noncopyable
	{
		//int (typename T::*)(char,float)
		typedef void (T::* fun_type)();

	public:
		
		scoped_memfun_guard (T* p, fun_type memberFun) :  
			_p(p), _memberFun (memberFun) 
		{  }

		~scoped_memfun_guard () {   
			release ();
		}

		T* operator->() const {
			return _p;
		}


		void reset() {
			_p = 0;
		}

		void release() {
			if (0 == _p)
				return;

			try {
				(_p->*_memberFun)();
			} catch(...) {
				// eat exception
			}
			
			_p = 0;
		}
	private:
		T* _p;
		fun_type _memberFun;
	};

	//==================================================================//
	template <typename OnDestroyFun, typename Param> 
	class scoped_param_guard : private boost::noncopyable
	{
	public:
		
		scoped_param_guard (OnDestroyFun proc, Param& p) 
			: _proc (proc), _p(p), _released (false) {  }
		
		~scoped_param_guard () {   
			release();
		}
		
		void reset () {
			_released = true;
		}

		void release () {   
			if (_released)
				return;

			try {
				_proc(_p);
			} catch(...) {
				// eat exception
			}

			_released = true;
		}

	private:
		OnDestroyFun _proc;
		Param& _p;

		bool _released;
	};

    //==================================================================//
	template <typename OnDestroyFun, typename Param, typename Param2> 
	class scoped_param_guard2 : private boost::noncopyable
	{
	public:
		
		scoped_param_guard2 (OnDestroyFun proc, Param& p, Param2& p2) : 
			_proc (proc), _p(p), _p2(p2) {  
		}

		~scoped_param_guard2 () {   
			try {
				_proc(_p, _p2);
			} catch(...) {
				// eat exception
			}
		}
	private:
		OnDestroyFun _proc;
		Param& _p;
		Param2& _p2;
	};

    //==================================================================//

	template <typename T, typename F> 
	class scoped_property_guard : private boost::noncopyable
	{
	public:
		
        scoped_property_guard (T* obj, F T::* member, F initialValue ) : 
            _obj (obj),
            _member (member) 
        {
            _obj->*_member = initialValue;
        }

        ~scoped_property_guard () 
        {
            try {
	            _obj->*_member = 0;	
            } catch(...) {
	            // eat exception
            }
        }
	private:
		T* _obj;
		F T::* _member;
	};

}}


#endif // ACONNECT_UTIL_GUARDS_H
